package pub.developers.forum.portal.controller.rest;

import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.message.MessagePageRequest;
import pub.developers.forum.api.response.message.MessagePageResponse;
import pub.developers.forum.api.service.MessageApiService;
import pub.developers.forum.common.constant.Constant;
import pub.developers.forum.portal.request.MessageRequest;
import pub.developers.forum.portal.support.WebUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;




@RestController
@RequestMapping("/message-rest")
public class MessageRestController {

    private static final String FOLLOW_USER = "用户关注";

    private static final String COMMENT_ARTICLE = "评论文章";
    private static final String APPROVAL_ARTICLE = "点赞文章";

    private static final String COMMENT_FAQ = "评论问答";
    private static final String APPROVAL_FAQ = "关注问答";


    private static final List<String> TYPE_LIST = Arrays.asList(FOLLOW_USER, COMMENT_ARTICLE, APPROVAL_ARTICLE, COMMENT_FAQ, APPROVAL_FAQ);

    @Resource
    private MessageApiService messageApiService;



    @PostMapping("/mark-is-read/{id}")
    public ResultModel delete(@PathVariable("id") Long id, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return messageApiService.markIsRead(id);
    }

    @PostMapping("/message")
    public ResultModel<List<MessagePageResponse>> message(@RequestBody MessageRequest messageRequest,HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        // 如果类型参数为空，默认为收到的关注信息
        if (ObjectUtils.isEmpty(messageRequest.getType())) {
            messageRequest.setType(TYPE_LIST.get(0));
        }
        return messageApiService.message();
    }
}
